package com.situ.servlet;

import com.situ.filter.LoginFilter;
import com.situ.pojo.User;
import com.situ.service.UserService;
import com.situ.service.impl.UserServiceImpl;
import com.situ.util.ServletUtils;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;

@WebServlet("/admin/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
        dispatcher.forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //身份认证
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String captcha = req.getParameter("captcha");
        User user = new User();
        //参数有效性验证
        if (!StringUtils.hasText( username)){
            ServletUtils.renderJson(resp, Map.of("success",false,"error","用户名不能为空"));
            return;
        }
        if (!StringUtils.hasText( password)){
            ServletUtils.renderJson(resp, Map.of("success",false,"error","密码不能为空"));
            return;
        }
        if (!StringUtils.hasText( captcha)){
            ServletUtils.renderJson(resp, Map.of("success",false,"error","验证码不能为空"));
            return;
        }
        //验证码是否正确
        HttpSession session = req.getSession();
        //验证码答案
        String answer=(String) session.getAttribute("captcha");
        if (!captcha.equalsIgnoreCase(answer)){
            ServletUtils.renderJson(resp, Map.of("success",false,"error","验证码错误"));
            return;
        }
        user.setUsername(username);
        user.setPassword(password);
        user.setCaptcha(captcha);
        //判断用户是否存在
        UserService userService = new UserServiceImpl();
        User user1 = userService.findByUsername(username);
        if (user1==null){
            ServletUtils.renderJson(resp, Map.of("success",false,"error","用户不存在"));
            return;
        }
        System.out.println(user1);
        //潘墩密码对错（加密）
        String encrypt= password+"{"+username+"}";
        //加密
        encrypt = DigestUtils.md5DigestAsHex(encrypt.getBytes());
        if (!encrypt.equals(user1.getPassword())){
            ServletUtils.renderJson(resp, Map.of("success",false,"error","密码错误"));
            return;
        }
        //重定向--main主页面
        req.getSession().setAttribute(LoginFilter.LOGIN_USER_KEY,user1);
        ServletUtils.renderJson(resp, Map.of("success",true));
    }
}
